<template>
	<div>jstest2</div>
</template>

<script>
	import {
		ref,
		reactive
	} from 'vue'
	export default {
		data() {
			return {}
		},
		mounted() {
			this.birthDay();
			this.objectSample();
			this.changeBoolean();
			this.includesTest();
			this.floorTest();
			this.oneLineTest();
		},
		methods: {
			birthDay() {
				console.log('使用扩展运算符将剩余元素分配给变量', '='.repeat(10))
				var student = {
					name: "Matt",
					age: 23,
					city: "Helsinki",
					state: "Finland",
				};
				var {
					name,
					age,
					...address
				} = student
				console.log('address', address)
			},

			objectSample() {
				console.log('你是否希望对象键与值具有相同的名称？你可以省略对象文字来执行此操作', '='.repeat(10))
				var name = "Luis",
					city = "Paris",
					age = 43,
					favoriteFood = "Spaghetti";
				var person = {
					name,
					city,
					age,
					favoriteFood
				}
				console.log('person', person)
			},

			changeBoolean() {
				console.log('使用 !! 或者Boolean将任何值转换为布尔值', '='.repeat(10))
				//!""把值转换成布尔类型，并取反。!!""再次取反，得到原本的不二类型状态
				console.log('!!""', !!"")
				console.log('Boolean("")', Boolean(""))
				console.log('!![]', !![])
				console.log('Boolean([])', Boolean([]))
			},

			includesTest() {
				console.log('使用 includes() 方法检查元素是否在数组中', '='.repeat(10))
				let numbers = [1, 2, 3];
				var hasNumber1 = numbers.indexOf(1) > -1
				var hasNumber2 = numbers.includes(1)
				console.log('hasNumber1', hasNumber1, 'hasNumber2', hasNumber2)

				console.log('使用 includes() 方法压缩多个条件', '='.repeat(10))
				var num = 1;
				if (num == 1 || num == 2 || num == 3) {
					console.log("Yay");
				}
				if ([1, 2, 3].includes(num)) {
					console.log("Yay");
				}
			},

			floorTest() {
				console.log('指数运算符', '='.repeat(10))
				var num1 = Math.pow(2, 3);
				var num2 = 2 ** 3
				console.log('num1', num1, 'num2', num2)
				console.log('四舍五入Math.floor()，也可以使用~~运算符', '='.repeat(10))
				var num1 = Math.floor(5.25);
				var num2 = ~~5.25
				console.log('num1', num1, 'num2', num2)
			},

			oneLineTest() {
				console.log('用一行代码分配多个值', '='.repeat(10))
				var num1, num2;
				// LONGER FORM
				num1 = 10;
				num2 = 100;
				// console.log('num1', num1, 'num2', num2)
				[num1, num2] = [1, 2]
				console.log('num1', num1, 'num2', num2)

				var student = {
					name: "Matt",
					age: 29,
				};
				var {
					name,
					age
				} = student
				console.log('name', name, 'age', age)

			},

			oneLineTest() {
				console.log('实现一个数组扁平化的函数flat', '='.repeat(10))

				function flatten(list, depth = 1) {
					if (depth == 0) return list
					return list.reduce((a, b) => a.concat(Array.isArray(b) ? flatten(b, depth - 1) : b), [])
				}
				var a = flatten([1, 2, 3, [4, [5, 6]]], 1)
				var b = flatten([1, 2, 3, [4, [5, 6]]], 2)
				console.log('a', a)
				console.log('b', b)
			},
			oneLineTest() {
				console.log('从数组中随机取一个元素', '='.repeat(10))

				var sample = function(arr) {
					return arr[Math.floor(Math.random() * arr.length)]
				}
				var arr=[1, 2, 3, 4, 5, 6]
				var arrSample=sample(arr)
				console.log('arrSample', arrSample)
			},

		},
	}
</script>

<style>
</style>
